package com.ice.work.study.other;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Maps;
import lombok.Data;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.text.StrSubstitutor;

import java.util.Map;

/**
 * @Description Java字符串占位符(commons - text)替换
 * @Author DELL
 * @Create 2019-12-24
 * @Since 1.0.0
 */
public class StringReplaceTest {

    public static void main(String[] args) {
        Map<String, Object> replaceValue = Maps.newHashMap();
        replaceValue.put("key1", 1);
        replaceValue.put("key1.key2", 2);
        StrSubstitutor strSubstitutor = new StrSubstitutor(replaceValue);
        String template = "${key1} is at the age of${key1.key2}";
        String result = strSubstitutor.replace(template);
        System.out.println(result);
        Person person = new Person();
        person.setName("12");
        person.setAge(null);
        Person children = new Person();
        children.setAge("333");
        children.setName("13");
        person.setChildren(children);
        replaceValue = BeanUtil.beanToMap(person);
        template = "${children} is at the age of${age}";
        strSubstitutor = new StrSubstitutor(replaceValue);
        result = strSubstitutor.replace(template);
        System.out.println(result);
    }
}

@Data
class Person {
    private String name;
    private String age;
    private Person children;
}
